var express = require('express');
var router = express.Router();
const loginCheck = require('../middleware/loginCheck.js');
const {
	login
} = require('../controller/user.js');
router.post('/login', function(req, res, next) {
	// 获取传入的post数据
	const {
		username,
		password
	} = req.body;
	console.log('账户密码：',username,password);
	login(username, password).then(e => {
		if (e.length != 0) {
			// 登陆成功
			// 设置session并且同步redis
			console.log('e',e[0].username);
			req.session.username = e[0].username;
			req.session.realname = e[0].realname;
			req.session.login = 'yes';
			console.log(req.session);
			res.json({
				errno: 0,
				data: e[0]
			});
		}else{
			res.json({
				errno: 0,
				data: '登陆失败'
			});
			return;
		}
	});

});

router.get('/viewnum',(req,res,next) => {
	const session = req.session;
	if(session.viewnum == null){
		session.viewnum = 0;
	}
	session.viewnum++;
	console.log('/viewnum',req.session);
	res.json({
		num:session.viewnum,
		data:{
			username:session.username,
			realname:session.realname
		}
	});
	// if(session.username){
	// 	res.json({
	// 		num:session.viewnum,
	// 		data:{
	// 			username:session.username,
	// 			realname:session.realname
	// 		}
	// 	});
	// }else{
	// 	res.json({
	// 		num:session.viewnum,
	// 		data:'没有登录'
	// 	});
	// }
});

module.exports = router;
